package com.zcx.project.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zcx.project.entity.ZcxEquipment;
import com.zcx.project.entity.ZcxUser;
import com.zcx.project.mapper.ZcxEquipmentMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcx.project.entity.ZcxEquipmentIn;
import com.zcx.project.mapper.ZcxEquipmentInMapper;


@Controller
@RequestMapping("/ZcxEquipmentIn")
public class ZcxEquipmentInController {

    @Autowired
    ZcxEquipmentInMapper zcxEquipmentInMapper;

    @Autowired
    ZcxEquipmentMapper zcxEquipmentMapper;

    @RequestMapping(value = "ZcxEquipmentInHtml")
    public ModelAndView getMainPage(HttpSession session, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("zcxEquipmentIn");
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/findList")
    public List findList(ZcxEquipmentIn obj) {
        List list = zcxEquipmentInMapper.findList(obj);
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/query")
    public Map<String, Object> findZcxEquipmentInPage(int currentPage, int rows, ZcxEquipmentIn obj) {

        //使用分页插件,核心代码就这一行，页数、每页行数
        PageHelper.startPage(currentPage, rows);
        List<ZcxEquipmentIn> list = zcxEquipmentInMapper.findList(obj);
        PageInfo<ZcxEquipmentIn> pageInfo = new PageInfo<ZcxEquipmentIn>(list);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageInfo.getTotal());
        result.put("rows", pageInfo.getList());
        return result;


    }


    @ResponseBody
    @RequestMapping(value = "/insertZcxEquipmentIn")
    public Map<String, Object> addZcxEquipmentIn(ZcxEquipmentIn obj, HttpSession session) {

        ZcxUser user = (ZcxUser) session.getAttribute("user");
        obj.setUserId(user.getId());
        int i = zcxEquipmentInMapper.insertSelective(obj);
        ZcxEquipment zcxEquipment = zcxEquipmentMapper.selectByPrimaryKey(obj.getEquipmentId());
        ZcxEquipment ze = new ZcxEquipment();
        ze.setId(obj.getEquipmentId());
        ze.setNum(zcxEquipment.getNum() + obj.getNum());
        zcxEquipmentMapper.updateByPrimaryKeySelective(ze);


        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "添加失败！");
        else
            returnMap.put("showInfo", "添加成功！");
        return returnMap;
    }


    @ResponseBody
    @RequestMapping(value = "/updateZcxEquipmentIn")
    public Map<String, Object> updateZcxEquipmentIn(ZcxEquipmentIn obj) {
        int i = zcxEquipmentInMapper.updateByPrimaryKeySelective(obj);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "修改失败！");
        else
            returnMap.put("showInfo", "修改成功！");
        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteZcxEquipmentIn")
    public Map<String, Object> deleteZcxEquipmentIn(int id) {
        int i = zcxEquipmentInMapper.deleteByPrimaryKey(id);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "删除失败！");
        else
            returnMap.put("showInfo", "删除成功！");
        return returnMap;
    }


}
